import org.dom4j.*;

import java.io.IOException;
import java.util.HashMap;

/**
 * @author ml
 * @create 2017-07-28--17:14
 */
public class MsgService_app {
    private String basePath;
    private String pfuserName;
    private String estateName;
    private String dataBaseIp;
    private String serverIp;
    public MsgService_app(String basePath, String pfuserName, String estateName, String dataBaseIp, String serverIp){
        this.basePath= basePath;
        this.pfuserName=pfuserName;
        this.estateName=estateName;
        this.dataBaseIp=dataBaseIp;
        this.serverIp=serverIp;



    }

    public void changexml() throws Exception{
        Document doc=XmlUtil.parse(basePath+FilePathEnum.MSGSERVICE_APPLICATIONCONTEXT.getValue());
        updateDocByXPATH(doc);

    }

    public void updateDocByXPATH(Document doc) throws DocumentException, IOException {

        Element rootElm = doc.getRootElement();

        System.out.println(rootElm.getName());

        HashMap map=new HashMap();
        map.put("design",rootElm.getNamespaceURI());


        XPath xPath=doc.createXPath("//design:bean[@id='dataSource']/design:property[@name='url']");

        xPath.setNamespaceURIs(map);

        Element element = (Element) xPath.selectSingleNode(doc);

        Attribute database=element.attribute("value");
        System.out.println(database.getName()+database.getValue());
        database.setValue("jdbc:oracle:thin:@"+dataBaseIp+":1521:orcl");

        XPath xPath2=doc.createXPath("//design:bean[@id='dataSource']/design:property[@name='username']");
        xPath2.setNamespaceURIs(map);

        Element element2 = (Element) xPath2.selectSingleNode(doc);
        Attribute username=element2.attribute("value");
        System.out.println(username.getName()+username.getValue());

        username.setValue(pfuserName);
        System.out.println(username.getValue());

        XmlUtil.saveDocument(doc,basePath+FilePathEnum.MSGSERVICE_APPLICATIONCONTEXT.getValue());







    }



}
